package com.midian.common.iosPush;

import java.util.List;
import java.util.Map;

import javapns.Push;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.Devices;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.PushQueue;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IosPush {
	private static final Logger Log = LoggerFactory.getLogger(IosPush.class);
	//public static String keystore = "/Users/xiaoling/Downloads/eclipse/www/commonfile/mujiang51.p12";
	public static String keystore = "/usr/ios_push/mujiang51.p12";
	public static String password = "12345678";
	public static String host = "gateway.sandbox.push.apple.com";
	public static Boolean production = false;// true：production false: sandbox
//	public static String host = "gateway.push.apple.com"; //生产环境地址
//	public static Boolean production = true;//生产环境
	public static final int numberOfThreads = 2;

	private static IosPush instance;
	PushQueue queue;

	public IosPush() {
		super();
		try {
			queue = Push.queue(keystore, password, production, numberOfThreads);
		} catch (KeystoreException e) {
			Log.error(e.getMessage(), e);
		}
		queue.start();
	}

	public static IosPush getInstance() {
		if (instance == null) {
			instance = new IosPush();
		}
		return instance;
	}	
	
	public void add(String deviceId, String msg, Integer badge, String sound,
			Map<String, String> map)throws JSONException {
		if(deviceId==null||"".equals(deviceId)){
			return;
		}
		String[] devs = new String[1];
		devs[0] = deviceId;
		List<Device> devices = Devices.asDevices(devs);
		PayloadPerDevice perDevice = new PayloadPerDevice(customPayload(msg,
				badge, sound, map), devices.get(0));
		queue.add(perDevice);
	}
	public void add(String deviceId,String msg, Integer badge) throws JSONException{
		if(deviceId==null||"".equals(deviceId)){
			return;
		}
		String[] devs = new String[1];
		devs[0] = deviceId;
		List<Device> devices = Devices.asDevices(devs);
		PayloadPerDevice perDevice = new PayloadPerDevice(customPayload(msg,
				badge, null, null), devices.get(0));
		queue.add(perDevice);
	}

	
	private static PushNotificationPayload customPayload(String msg,
			Integer badge, String sound, Map<String, String> map)
			throws JSONException {
		PushNotificationPayload payload = PushNotificationPayload.complex();
		if (StringUtils.isNotEmpty(msg)) {
			payload.addAlert(msg);
		}
		if (badge != null) {
			payload.addBadge(badge);
		}
		payload.addSound(StringUtils.defaultIfEmpty(sound, "default"));
		if (map != null && !map.isEmpty()) {
			Object[] keys = map.keySet().toArray();
			Object[] vals = map.values().toArray();
			if (keys != null && vals != null && keys.length == vals.length) {
				for (int i = 0; i < map.size(); i++) {
					payload.addCustomDictionary(String.valueOf(keys[i]),
							String.valueOf(vals[i]));
				}
			}
		}
		return payload;
	}
//	public static void main(String[] args) throws InterruptedException {
//		IosPush push = new IosPush();
//		try {
//			push.add("70db3434451db5ad72a799c5a4101a18f634683f90b3ce93b9b462d10d185ab5", "隔壁老王，隔壁老王", 1);
//			Thread.sleep(5000);
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
//	}

}
